<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
		<title>MVC DOH Test</title>

		<style>
			@import "../../../../dojo/resources/dojo.css";
			@import "../../../../dijit/tests/css/dijitTests.css";
			@import "../css/app-format.css";
		</style>

		<!-- required: the default dijit theme: -->
		<link id="themeStyles" rel="stylesheet" href="../../../../dijit/themes/claro/claro.css">

		<!-- required: dojo.js -->
		<script src="../../../../dojo/dojo.js" type="text/javascript" data-dojo-config="parseOnLoad: 0, isDebug: 1, mvc: {debugBindings: 1}"></script>
		<script type="text/javascript" src="./helpers.js"></script>

		<script type="text/javascript" >
			require([
				"doh/runner",
				"dojo/_base/declare",
				"dojo/when",
				"dojo/dom",
				"dojo/parser",
				"dojo/promise/all",
				"dojo/store/JsonRest",
				"dijit/registry",
				"dijit/_WidgetBase",
				"dojox/mvc/getStateful",
				"dojox/mvc/EditModelRefController",
				"dojox/mvc/ListController",
				"dijit/form/TextBox",
				"dijit/form/Button",
				"dojox/mvc/Group",
				"dojox/mvc/Repeat",
				"dojo/domReady!"
			], function(doh, declare, when, ddom, parser, all, JsonRest, registry, _WidgetBase, getStateful, EditModelRefController, ListController){

				var ctrlClass = declare([_WidgetBase, EditModelRefController, ListController], {});
				(ctrl = (new ctrlClass({srcNodeRef: ddom.byId("detailsGroup"), cursorIndex: 0}))).startup();

				when(all([parser.parse(), new JsonRest({target: require.toUrl("dojox/mvc/tests/_data/mvcRepeatData.json")}).query({})]), function(a){
					ctrl.set("sourceModel", getStateful(a[1].items));

					 // should be able to verify all of the inputs
					doh.register("Check values and bindings", [
						{
							name: "Initial",
							runTest: function(){
								doh.is("Anne", registry.byId("nameInput0").value, "nameInput0 should be Anne");
								doh.is("Anne", registry.byId("firstInput").value, "firstInput should be Anne");
							}
						}, {
							name: "TestBen",
							runTest: function(){
								ctrl.set("cursorIndex", 1);
								doh.is("Ben", registry.byId("nameInput1").value, "nameInput1 should be Ben");
								doh.is("Ben", registry.byId("firstInput").value, "firstInput should be Ben");
							}
						}, {
							name: "TestJohn",
							runTest: function(){
								ctrl.set("cursorIndex", 9);
								doh.is("John", registry.byId("nameInput9").value, "nameInput9 should be John");
								doh.is("John", registry.byId("firstInput").value, "firstInput should be John");

								registry.byId("firstInput").set("value", "Johnny");

								doh.is("Johnny", ctrl.get("model").get(9).get("First"), "First name of the 10th entry should be changed to Johnny");
							}
						}, {
							name: "TestCommitReset",
							runTest: function(){
								ctrl.set("cursorIndex", 8);

								doh.is("Irene", registry.byId("nameInput8").value, "nameInput8 should be Irene");
								doh.is("Irene", registry.byId("firstInput").value, "firstInput should be Irene");

								registry.byId("firstInput").set("value", "IreneThisUpdateShouldBeSaved");

								ctrl.commit();

								registry.byId("lastInput").set("value", "IraThisUpdateShouldBeReset");

								ctrl.reset();

								doh.is("IreneThisUpdateShouldBeSaved", registry.byId("nameInput8").value, "nameInput8 should be IreneThisUpdateShouldBeSaved");
								doh.is("IreneThisUpdateShouldBeSaved", registry.byId("firstInput").value, "firstInput should be IreneThisUpdateShouldBeSaved");
								doh.is("Ira", registry.byId("lastInput").value, "lastInput should be Ira");
							}
						}
					]);

					doh.run();
				});
			});
		</script>
	</head>
	<body class="claro">
		<script type="dojo/require">at: "dojox/mvc/at"</script>
		<h1 class="testTitle">doh_mvc_programmatic-repeat-store.html automated tests (non-robot)</h1>
		<div id="wrapper">
			<div id="header">
				<div id="navigation"></div>
				<div id="headerInsert">
					<h2>Master Detail Example - With repeat container, using a store, with save, commit and reset.</h2>
				</div>
			</div>
			<div id="main">
				<div id="leftNav"></div>
				<div id="mainContent">
					<div id="searchBanner">Search Results for term: </div>
					<table>
						<tbody>
							<tr>
								<td>
									<div>
										<div>Declarative Repeat using my.Repeat does not pass templateString: </div>
										<div id="repeatId" data-dojo-type="dojox.mvc.Repeat" data-dojo-props="children: at(ctrl, 'model')">
											<div class="row" data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at('rel:', '${this.index}')">
												<label class="cell" for="nameInput${this.index}">Name:</label>
												<input class="cell" data-dojo-type="dijit.form.TextBox" id="nameInput${this.index}" data-dojo-props="value: at('rel:', 'First')"/>
												<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){ ctrl.set('cursorIndex', '${this.index}'); }">Details</button>
											</div>
										</div>
									</div>
								</td>
							</tr>
						</tbody>
					</table>
					<div class="spacer"></div>
					<div id="detailsBanner">Details for selected index:</div>
					<div id="detailsGroup">
						<div class="row">
							<label class="cell" for="firstInput">First Name:</label>
							<input class="cell" id="firstInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'First')">
						</div>
						<div class="row">
							<label class="cell" for="lastInput">Last Name:</label>
							<input class="cell" id="lastInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Last')">
						</div>
						<div class="row">
							<label class="cell" for="locationInput">Location:</label>
							<input class="cell" id="locationInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Location')">
						</div>
						<div class="row">
							<label class="cell" for="officeInput">Office:</label>
							<input class="cell" id="officeInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Office')">
						</div>
						<div class="row">
							<label class="cell" for="emailInput">Email:</label>
							<input class="cell" id="emailInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Email')">
						</div>
						<div class="row">
							<label class="cell" for="telInput">Telephone:</label>
							<input class="cell" id="telInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Tel')">
						</div>
						<div class="row">
							<label class="cell" for="faxInput">Fax:</label>
							<input class="cell" id="faxInput" data-dojo-type="dijit.form.TextBox" data-dojo-props="value: at('rel:', 'Fax')">
						</div>
						<div class="row">
						<div class="spacer"></div>
							<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){ ctrl.commit();}">Commit All</button>
							<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){ ctrl.reset(); }">Reset to last saved</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
